package com.guoli.sms.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.guoli.sms.service.IDeptService;
import com.guoli.sms.service.IUserPhoneService;
import com.guoli.sms.service.impl.DeptServiceImpl;
import com.guoli.sms.service.impl.UserPhoneServiceImpl;
import com.guoli.sms.util.DwzjsonTool;
import com.guoli.sms.vo.Dept;
import com.guoli.sms.vo.DwzJson;
import com.guoli.sms.vo.SmsSession;
import com.guoli.sms.vo.Sysuser;
import com.guoli.sms.vo.UserPhone;


/**
 * @author zhuxiaodong
 *
 */
public class UserPhoneServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Sysuser sysuser = (Sysuser) request.getSession().getAttribute("sysuser");
		SmsSession smsSession = (SmsSession) request.getSession().getAttribute("smssession");
		if(smsSession == null){
			request.getSession().setAttribute("smssession", new SmsSession());
			smsSession = (SmsSession) request.getSession().getAttribute("smssession");
		}
		IDeptService deptService = new DeptServiceImpl();
		IUserPhoneService userPhoneService = new UserPhoneServiceImpl();
		String opt = request.getParameter("opt");
		if(sysuser!=null){
			if("uplist".equals(opt)){
				List<Dept> depts = deptService.getDepts();
				request.setAttribute("depts", depts);
				int dept_id = smsSession==null? 0 : smsSession.getDept_id();
				
//				List<UserPhone> userPhones = userPhoneService.getUserPhonesByDN(dept_id, "");
				List<UserPhone> userPhones = userPhoneService.getUserPhonesByDNCS(dept_id, "", "0", "");
				request.setAttribute("userPhones", userPhones);
				List<String> compNames = userPhoneService.getCompanyNames();
				request.setAttribute("compNames", compNames);
				
				request.getRequestDispatcher("/WEB-INF/jsp/userphonelist.jsp").forward(request, response);
			}
			if("userfilter".equals(opt)){
				List<Dept> depts = deptService.getDepts();
				request.setAttribute("depts", depts);
				int dept_id = Integer.parseInt(request.getParameter("dept_id"));
				String u_company = request.getParameter("company");
				String u_phoneno = request.getParameter("u_phoneno");
				String u_name = request.getParameter("u_name").trim();
				if(smsSession != null){
					smsSession.setDept_id(dept_id);
					smsSession.setU_name(u_name);
					smsSession.setU_company(u_company);
					smsSession.setU_phoneno(u_phoneno);
				}
				
//				List<UserPhone> userPhones = userPhoneService.getUserPhonesByDN(dept_id, u_name);
				List<UserPhone> userPhones = userPhoneService.getUserPhonesByDNCS(dept_id, u_name, u_company, u_phoneno);
				request.setAttribute("userPhones", userPhones);
				
				List<String> compNames = userPhoneService.getCompanyNames();
				request.setAttribute("compNames", compNames);
				
				request.getRequestDispatcher("/WEB-INF/jsp/userphonelist.jsp").forward(request, response);
			}
			if("notelist".equals(opt)){
				@SuppressWarnings("unchecked")
				List<UserPhone> userPhones = (List<UserPhone>) request.getSession().getAttribute("note");
				if(userPhones != null ){
					request.setAttribute("note", userPhones);
				}
				request.getRequestDispatcher("/WEB-INF/jsp/notelist.jsp").forward(request, response);
			}
			if("upadd".equals(opt)){
				List<Dept> depts = deptService.getDepts();
				request.setAttribute("depts", depts);
				request.getRequestDispatcher("/WEB-INF/jsp/upadd.jsp").forward(request, response);
			}
			if("upedit".equals(opt)){
				int up_id = Integer.parseInt(request.getParameter("up_id"));
				UserPhone userPhone = userPhoneService.getUserPhoneById(up_id);
				List<Dept> depts = deptService.getDepts();
				request.setAttribute("depts", depts);
				request.setAttribute("userPhone", userPhone);
				request.getRequestDispatcher("/WEB-INF/jsp/upedit.jsp").forward(request, response);
			}
			if("deptadd".equals(opt)){
				request.getRequestDispatcher("/WEB-INF/jsp/deptadd.jsp").forward(request, response);
			}
		}
		else{
			response.setContentType("text/json");
			DwzJson dwzJson = DwzjsonTool.newInstance().initDwzjson("301", "登录超时，请重新登录！", "", "", "", "");
			response.getWriter().write(new Gson().toJson(dwzJson));
		}
	}

}
